/*****************************************************************************
Module :     ThreadSchedule.CPP
Notices:     2007.3.1 Written by Ling, Xiao-li.  Mailto: ling.xiaoli.s@gmail.com
Description: This file encapsulates schedule of travian tasks in a extra thread.
			 
*****************************************************************************/

// ThreadSchedule.cpp : implementation file
//

#include "stdafx.h"
#include "TravianAutoTask.h"
#include "ThreadSchedule.h"


// CThreadSchedule

//namespace Travian
//{

IMPLEMENT_DYNCREATE(CThreadSchedule, CWinThread)

CThreadSchedule::CThreadSchedule()	
{	 
}

CThreadSchedule::~CThreadSchedule()
{
}

BOOL CThreadSchedule::InitInstance()
{
	// TODO:  perform and per-thread initialization here

	if (!AfxOleInit())
		return FALSE;

	return TRUE;
	// Can't call parent class CWinThread::InitInstance(), otherwise message map can't work.
}

int CThreadSchedule::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	m_TTS.Cleanup();
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CThreadSchedule, CWinThread)
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_SETPARAM,		OnScheduleSetParam)
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_INIT,			OnScheduleInit)		
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_START,			OnScheduleStart)	
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_STOP,			OnScheduleStop)	
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_STARTATTACK,	OnScheduleStartAttackTimer)	
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_CLEAN_ATTACK,	OnCleanAttack)	
	ON_THREAD_MESSAGE(WM_THREADMSG_SCHEDULE_ERASE_ATTACK,	OnEreaseAttackTimer)	
END_MESSAGE_MAP()


//
// CThreadSchedule message handlers
//

afx_msg void CThreadSchedule::OnScheduleInit(WPARAM wParam, LPARAM lParam) 
{
	//OnScheduleSetParam(wParam, lParam);
	m_TTS.Init();
};

afx_msg void CThreadSchedule::OnScheduleSetParam(WPARAM wParam, LPARAM lParam) 
{
	//m_TTS.SetTaskFinishAction ( (eTravianTaskFinishAction) wParam);

	// lParam is the pointer of parameter object

	bool bTasksSetRet = false;
	bool bTasksFile = false;
	ScheduleParam* pParam = (ScheduleParam*)lParam;

	if (pParam->ulFieldSetFlag & (1<<7))				// pEvtLog
		m_TTS.SetEventLog(pParam->pEvtLog);

	if (pParam->ulFieldSetFlag & (1<<0))				// hWnd
		m_TTS.SetWnd2Notify(pParam->hWnd);

	if (pParam->ulFieldSetFlag & (1<<1))				// Svr
		m_TTS.SetSvr(pParam->sSvr);

	if (pParam->ulFieldSetFlag & ( (1<<2) | (1<<3)) )	// User&Pwd
		m_TTS.SetUserPwd(pParam->sUser, pParam->sPwd);

	if (pParam->ulFieldSetFlag & (1<<4))				// sTaskFileName
	{
		bTasksSetRet = m_TTS.SetTasks(pParam->sTaskFileName);
		bTasksFile = true;
	}

	if (pParam->ulFieldSetFlag & (1<<5))				// ts
		m_TTS.SetTasks(pParam->ts);

	if (pParam->ulFieldSetFlag & (1<<6))				// eAction
		m_TTS.SetTaskFinishAction(pParam->eAction);

	if (pParam->ulFieldSetFlag & (1<<8))				// m_tasksAttack
		m_TTS.SetAttackTasks(pParam->ts);
	else {
		Tasks* pts = NULL;
		if (bTasksSetRet)
			pts = new Tasks( CTravianTask::CloneTasks(m_TTS.GetTasks()) );
		if (m_TTS.GetWnd2Notify())
		{
			if (bTasksFile)
				PostMessage(m_TTS.GetWnd2Notify(), WM_USERMSG_TASK_SETPARAM_FINISHED, ((int)bTasksSetRet), (LPARAM)pts);
			else
				PostMessage(m_TTS.GetWnd2Notify(), WM_USERMSG_TASK_SETPARAM_FINISHED, 1, (LPARAM)pts);
		}
	}

	delete pParam;
};
afx_msg void CThreadSchedule::OnScheduleStart(WPARAM wParam, LPARAM lParam) 
{
	m_TTS.Start();
};

afx_msg void CThreadSchedule::OnScheduleStartAttackTimer(WPARAM wParam, LPARAM lParam) 
{
	m_TTS.StartAttackTimer(UINT(wParam),UINT(lParam));
};

afx_msg void CThreadSchedule::OnEreaseAttackTimer(WPARAM wParam, LPARAM lParam) 
{
	m_TTS.EreaseAttackTimer(UINT(wParam));
};

afx_msg void CThreadSchedule::OnScheduleStop(WPARAM wParam, LPARAM lParam) 
{
	m_TTS.KillTimers();
#ifdef _DEBUG
	m_TTS.KillAllAttackTimer();
#endif // _DEBUG
};

afx_msg void CThreadSchedule::OnCleanAttack(WPARAM wParam, LPARAM lParam) 
{
	m_TTS.KillAllAttackTimer();
	m_TTS.ClearAllAttackTasks();
};

//}